package top.wecreate.snet.demo;

import java.util.HashMap;
import java.util.Map;

import top.wecreate.snet.R;
import top.wecreate.snet.SnetBaseConnection.ConnetionBuilder;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView textView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView = (TextView) findViewById(R.id.text);
		
		
		// 1. 构建请求所需的参数，比如url，参数之类的
		ConnetionBuilder builder = new ConnetionBuilder(
				"http://www.wecreate.top/server.php");
		Map<String, String> params = new HashMap<>();
		params.put("action", "adduserinfo");
		builder.setRequestParams(params);
		
		// 2. 通过builder来构建请求。
		DemoSnetConnection connection = new DemoSnetConnection(builder) {

			@Override
			public void onSuccess(String result) {
				// 成功的值，此时在UI线程中
				textView.setTextColor(Color.BLUE);
				textView.setText(result);
			}

			@Override
			public void onFail(DemoErroInfo result) {
				// 失败的值，此时在UI线程中
				textView.setTextColor(Color.RED);
				textView.setText(result.msg);
			}
		};
		// 3. 正式发起请求。两种请求方式
		// connection.post();
		connection.get();
	
		//取消请求
		//connection.cancel();
	}

}
